//
//  JXHpopoverViewAnimation.swift
//  JXHSinaWeiBo
//
//  Created by juxiaohui on 16/10/18.
//  Copyright © 2016年 juxiaohui. All rights reserved.
//

import UIKit

class JXHpopoverViewAnimation: NSObject {

    var isPresented = false
    
    var presentFrame = CGRect.init()
    
    var animationStateBlock : ((_ presented : Bool)->())?
    
    
    // MARK: - 自定义构造函数
    init(animationStateBlock : ((_ presented : Bool)->())?) {
        
        self.animationStateBlock = animationStateBlock
    }
    
}

// MARK: - 自定义转场的代理方法
extension JXHpopoverViewAnimation : UIViewControllerTransitioningDelegate{
    
    ///改变转场的尺寸
    func presentationController(forPresented presented: UIViewController, presenting: UIViewController?, source: UIViewController) -> UIPresentationController? {
        
        let presentation = JXHCustomPresentationController.init(presentedViewController: presented, presenting: presenting)
        
        presentation.presentFrame = presentFrame
        
        return presentation
    }
    
    //返回遵守该协议的对象
    
    ///自定义弹出动画
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresented = true
        animationStateBlock!(isPresented)
        
        return self
    }
    
    ///自定义消失动画
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresented = false
        animationStateBlock!(isPresented)
        return self
    }
}

// MARK: - 弹出和消失动画的代理方法
extension JXHpopoverViewAnimation : UIViewControllerAnimatedTransitioning {
    
    ///动画的执行时间
    func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        return 0.5
    }
    
    ///获取转场的上下文，可以通过转场上下文获取弹出和消失的View
    func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        isPresented ? animationForPresentedView(using: transitionContext) : animationForDissmissedView(using: transitionContext)
        
    }
    ///弹出
    func animationForPresentedView(using transitionContext: UIViewControllerContextTransitioning) {
        //1.获取弹出的view
        let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to)!
        //2.将弹出的view添加到containerView
        transitionContext.containerView.addSubview(presentedView)
        //3.执行动画
        presentedView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0)
        presentedView.layer.anchorPoint = CGPoint.init(x: 0.5, y: 0)
        
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            presentedView.transform = CGAffineTransform.identity
            
        }) { (_) in
            //告诉上下文动画已完成
            transitionContext.completeTransition(true)
        }
    }
    
    
    ///消失
    func animationForDissmissedView(using transitionContext: UIViewControllerContextTransitioning) {
        //1.获取弹出的view
        let dissmissedView = transitionContext.view(forKey: UITransitionContextViewKey.from)!
        UIView.animate(withDuration: transitionDuration(using: transitionContext), animations: {
            dissmissedView.transform = CGAffineTransform.init(scaleX: 1.0, y: 0.0001)
        }) { (_) in
            //告诉上下文动画已完成
            transitionContext.completeTransition(true)
        }
    }
}
